package participants;

import java.util.ArrayList;

import org.resotc.model.Car;
import org.resotc.model.Centroid;
import org.resotc.model.Element;
import org.resotc.model.IndividualType;
import org.resotc.model.NeighbourOrientation;

import config.Configuration;

public class CarImpl extends IndividualImpl implements Car {

	public double xDir;
	public double yDir;
	public double lastxDir;
	public double lastyDir;
	public boolean randomize = false;
	ArrayList<Element> elements;

	public CarImpl(String SSID, double x, double y, double beginSimulationTime,Centroid mystart) {
		super(SSID, x, y, beginSimulationTime, IndividualType.CAR, mystart);
		this.setDesiredSpeed(Configuration.desiredCarSpeed);

		// System.out.println("Created new Car " + this.myID +
		// ": Starting at Centroid " + start.getID() + " and going to Centroid "
		// + destination.getID());

		this.xDir = x;
		this.yDir = y;
	}

	public double getXDir() {
		return xDir;
	}

	public double getYDir() {
		return yDir;
	}

	public boolean getRandomize() {
		return randomize;
	}

	public void setRandomize(boolean randomize) {
		this.randomize = randomize;
	}

	public NeighbourOrientation getCurrentOrientation() {
		double diff = 0;
		if (this.lastxDir == this.xDir) {
			diff = this.lastyDir - this.yDir;
			if (diff > 0) {
				return NeighbourOrientation.NORTH;
			} else {
				return NeighbourOrientation.SOUTH;
			}
		} else {
			diff = this.lastxDir - this.xDir;
			if (diff > 0) {
				return NeighbourOrientation.WEST;
			} else {
				return NeighbourOrientation.EAST;
			}

		}
	}

	// @Override
	// protected Element selectElement(ArrayList<Element> nextElements) {
	// // No error handling - done by super class
	// Element myNextStep;
	//
	// /**
	// * ToDo Implement method according to route choice model!
	// */
	//
	// // Work around:
	// myNextStep = nextElements.get(0);
	//
	// return myNextStep;
	// }

	// public void step(SimState state) {

	// ResOTCModel resOTCModel = (ResOTCModel) state;
	// // Must be Int2D because the function 'getObjectLocation()' delivers
	// // only Int2D.
	//
	// Double2D location = resOTCModel.cars.getObjectLocationAsDouble2D(this);
	// updatePosition(elements,location);
	// this.lastxDir = location.x;
	// this.lastyDir = location.y;
	// double newX = 1;
	// double newY = 1;
	// // At the very beginning of this project i work with randomized
	//
	// xDir = 0;
	// yDir = 0;
	// // move
	//
	// if (currentElement.getElementType() == ElementType.CENTROID ||
	// currentElement.getElementType() == ElementType.INTERSECTION) {
	// System.out.println("juhuu");
	//
	// HashMap<NeighbourOrientation, Element> neighbours =
	// currentElement.determineNeighbours();
	//
	// Random generator = new Random();
	// List<NeighbourOrientation> keys = new
	// ArrayList<NeighbourOrientation>(neighbours.keySet());
	//
	// NeighbourOrientation randomOrientation = keys.get(
	// generator.nextInt(keys.size()) );
	//
	// if (randomOrientation == NeighbourOrientation.EAST) {
	// System.out.println("east");
	// xDir = 1;
	// yDir = 0;
	// }
	// else if (randomOrientation == NeighbourOrientation.WEST) {
	// System.out.println("west");
	// xDir = -1;
	// yDir = 0;
	// }
	// else if (randomOrientation == NeighbourOrientation.SOUTH) {
	// System.out.println("south");
	// xDir = 0;
	// yDir = 1;
	// }
	// else if (randomOrientation == NeighbourOrientation.NORTH) {
	// System.out.println("north");
	// xDir = 0;
	// yDir = -1;
	// }
	// else{
	// System.err.println("nix gefunden");
	// }
	// }
	// newX = location.x + xDir;
	// newY = location.y + yDir;
	//
	//
	// if (currentElement.getElementType() == ElementType.STREET) {
	// Street currentstreet = (Street) currentElement;
	// if (currentstreet.getOrientation() == Orientation.HORIZONTAL) {
	// System.out.println("horizontal");
	// newX = location.x + 1;
	// } else {
	// System.out.println("vertical");
	// newY = location.y + 1;
	// }
	// }
	//
	//
	// System.out.println(location.x+" xcoord step");
	// System.out.println(location.y+" ycoord step");
	// System.out.println(newX+"new x");
	// System.out.println(newY+"new y");
	// // reverse course if hitting boundary
	// if (newX < 0) {
	// newX++;
	// xDir = -xDir;
	// }
	// if (newY < 0) {
	// newY++;
	// yDir = -yDir;
	// }
	//
	// this.x = newX;
	// this.y = newY;
	// // set new location
	// Double2D newLoc = new Double2D(newX, newY);
	// resOTCModel.cars.setObjectLocation(this, newLoc);
	//
	// // Bag c = resOTCModel.cars.getObjectsAtLocation(newLoc);
	// // System.out.println(c);
	// // if (c.numObjs > 1) {
	// // for (int x = 0; x < c.numObjs; x++) {
	// // ((Car) (c.objs[x])).randomize = true;
	// // }
	// // }
	// }

	public void setNewPosition(double x, double y) {
		this.x = x;
		this.y = y;
	}

	@Override
	protected Element selectElement(ArrayList<Element> nextElements) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public double calculateDistanceInStep() {
		// TODO Auto-generated method stub
		return 0;
	}

}
